package com.futonredemption.callmelater;

import org.beryl.app.AndroidVersion;
import org.beryl.diagnostics.Logger;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.QuickContactBadge;

public class MainActivity extends Activity {
    
	private static final int REQUESTCODE_PICKCONTACT = 1;
	Uri contactUri;
	
	LinearLayout ActionsPanel;
	Button PickContactButton;
	Button ViewContactButton;
	Button EditContactButton;
	QuickContactBadge ContactBadge;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindViews();
        
        if(savedInstanceState != null) {
        	restoreState(savedInstanceState);
        }
    }

	private void bindViews() {
		ActionsPanel = (LinearLayout)findViewById(R.id.ActionsPanel);
    	PickContactButton = (Button)findViewById(R.id.PickContactButton);
    	PickContactButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				requestContact();
			}
    	});
    	
    	ViewContactButton = (Button)findViewById(R.id.ViewContactButton);
    	ViewContactButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				viewContact();
			}
    	});
    	
    	EditContactButton = (Button)findViewById(R.id.EditContactButton);
    	EditContactButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				editContact();
			}
    	});

    	ContactBadge = (QuickContactBadge)findViewById(R.id.ContactBadge);
    	if(AndroidVersion.isHoneycombOrHigher()) {
    		ContactBadge.setImageToDefault();
    	}
    }
    
    private void requestContact() {
    	Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    	intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    	startActivityForResult(intent, REQUESTCODE_PICKCONTACT);
    }
    
    private void viewContact() {
    	Intent intent = new Intent(Intent.ACTION_VIEW);
    	intent.setDataAndType(contactUri, ContactsContract.Contacts.CONTENT_ITEM_TYPE);
    	startActivity(Intent.createChooser(intent, "Select One"));
    }
    
    private void editContact() {
    	Intent intent = new Intent(Intent.ACTION_EDIT);
    	intent.setDataAndType(contactUri, ContactsContract.Contacts.CONTENT_ITEM_TYPE);
    	startActivity(Intent.createChooser(intent, "Select One"));
    }

    @Override  
    public void onActivityResult(int requestCode, int resultCode, Intent data) {  
        super.onActivityResult(requestCode, resultCode, data);
        
        if(requestCode == REQUESTCODE_PICKCONTACT && resultCode == Activity.RESULT_OK) {
        	obtainContact(data);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
    	super.onSaveInstanceState(outState);
    	outState.putParcelable("contactUri", contactUri);
    }
    
    private void restoreState(Bundle savedInstanceState) {
    	Uri uri = savedInstanceState.getParcelable("contactUri");
    	loadContact(uri);
	}

	private void obtainContact(Intent data) {
		Logger.d(data);
		loadContact(data.getData());
	}

	private void loadContact(Uri data) {
		this.contactUri = data;
		ContactBadge.assignContactUri(this.contactUri);

		if(this.contactUri == null) {
			ActionsPanel.setVisibility(View.GONE);
		} else {
			ActionsPanel.setVisibility(View.VISIBLE);
		}
	}
}